package com.pureut.order.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.order.dto.ProcurementPlanDto;
import com.pureut.order.service.ProcurementPlanService;
import com.pureut.order.vo.OrderReviewVo;
import com.pureut.order.vo.ProcurementPlanUpdateVo;
import com.pureut.order.vo.ProcurementPlanVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

/**
 * 采购计划控制层
 */
@RestController
@RequestMapping("/procurement-plan")
public class ProcurementPlanController extends BaseController {

    @Resource
    ProcurementPlanService procurementPlanService;


    /**
     * 采购计划列表分页查询
     */
    @RequiresPermissions("order:purchase:plan:list")
    @GetMapping("/list")
    public TableDataInfo list(ProcurementPlanVo procurementPlanVo) {
        startPage();
        //查询采购计划列表
        List<ProcurementPlanDto> list = procurementPlanService.getList(procurementPlanVo);
        return getDataTable(list);
    }

    /**
     * 添加单据
     */
    @RequiresPermissions("order:purchase:plan:add")
    @Log(title = "添加单据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody ProcurementPlanVo procurementPlanVo) throws Exception {
        return toAjax(procurementPlanService.addProcurementPlan(procurementPlanVo));
    }

    /**
     * 修改采购计划
     */
    @RequiresPermissions("order:purchase:plan:edit")
    @Log(title = "修改采购计划", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody ProcurementPlanUpdateVo procurementPlanUpdateVo) {
        return toAjax(procurementPlanService.updateProcurementPlan(procurementPlanUpdateVo));
    }

    /**
     * 提交审核
     */
    @RequiresPermissions("order:purchase:plan:subExamine")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/submitReview/{id}")
    public AjaxResult submitReview(@PathVariable("id") Long id){
        return toAjax(procurementPlanService.submitReview(id));
    }

    /**
     *撤销审核
     */
    @RequiresPermissions("order:purchase:plan:revoke")
    @Log(title = "撤销审核", businessType = BusinessType.UPDATE)
    @GetMapping("/deleteReview/{id}")
    public AjaxResult deleteReview(@PathVariable("id") Long id){
        return toAjax(procurementPlanService.deleteReview(id));
    }

    /**
     * 关结采购计划
     */
    @RequiresPermissions("order:purchase:plan:closing")
    @Log(title = "关结采购计划", businessType = BusinessType.UPDATE)
    @PostMapping("/closePlan")
    public AjaxResult closePlan(@RequestBody ProcurementPlanVo procurementPlanVo){
        return toAjax(procurementPlanService.closePlan(procurementPlanVo));
    }

    /**
     * 查看采购计划
     */
    @RequiresPermissions("order:purchase:plan:query")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) {
        return AjaxResult.success(procurementPlanService.getPlanById(id));
    }


    /**
     * 删除采购计划
     */
    @RequiresPermissions("order:purchase:plan:remove")
    @Log(title = "删除采购计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable("ids") String ids){
        return toAjax(procurementPlanService.deleteProcurementPlan(ids));
    }

    /**
     * 撤回采购计划
     */
    @RequiresPermissions("order:purchase:plan:backPlan")
    @Log(title = "撤回采购计划", businessType = BusinessType.UPDATE)
    @GetMapping("/backPlan/{id}")
    public AjaxResult backPlan(@PathVariable("id") Long id) throws SQLException {
        return toAjax(procurementPlanService.backPlan(id));
    }

    /**
     * 审核单据
     */
    @RequiresPermissions("order:purchase:plan:review")
    @Log(title = "审核单据", businessType = BusinessType.UPDATE)
    @PostMapping("/purchaseReview")
    public AjaxResult purchaseReview(@Validated @RequestBody OrderReviewVo orderReviewVo) {
        return toAjax(procurementPlanService.purchaseReview(orderReviewVo));
    }

}
